<!DOCTYPE html>
<script src="../../resources/js-test.js"></script>
<base href="foo/bar/">
<body></body>
<script>

if (window.testRunner) {
    window.jsTestIsAsync = true;
    testRunner.waitUntilDone();
    testRunner.setPopupBlockingEnabled(false);
}

function endsWith(string, substring)
{
    var length = string.length - substring.length;
    return length >= 0 && string.indexOf(substring, length) === length;
}

var base = document.querySelector('base');
shouldBeTrue("endsWith(document.querySelector('base').href, 'foo/bar/')");
shouldBeFalse("endsWith(document.querySelector('base').href, 'foo/bar/foo/bar/')");

base.href = null;
shouldBeTrue("endsWith(document.querySelector('base').href, '/null')");

// When a document does not have a URL, base cannot be resolved

// Make sure that we use the creator document as the base.
var documentWithoutAView = document.implementation.createHTMLDocument('');
base = documentWithoutAView.head.appendChild(documentWithoutAView.createElement('base'));
base.setAttribute('href', 'foo/bar/');
shouldBeTrue("endsWith(documentWithoutAView.querySelector('base').href, 'foo/bar/')");
base.setAttribute('href', 'http://webkit.org/');
shouldBeEqualToString("documentWithoutAView.querySelector('base').href", 'http://webkit.org/');

// Make sure that we use the parent document as the base.
var iframe = document.body.appendChild(document.createElement('iframe'));
base = iframe.contentDocument.head.appendChild(iframe.contentDocument.createElement('base'));
base.setAttribute('href', 'foo/bar/');
shouldBeTrue("endsWith(iframe.contentDocument.querySelector('base').href, 'foo/bar/')");

// Make sure that we don't use the opener document as the base.
var newWindow = window.open('about:blank');
base = newWindow.document.head.appendChild(newWindow.document.createElement('base'));
base.setAttribute('href', 'foo/bar/');
shouldBeEqualToString("newWindow.document.querySelector('base').href", '');
newWindow.close();

</script>
<script>

finishJSTest();

</script>
